package cn.hbu.zlfw.common.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hbu.zlfw.admin.entity.ScienceExpert;
import cn.hbu.zlfw.aspect.service.OperationLogService;
import cn.hbu.zlfw.patent.entity.Patent;

/**
 * 
 * @ClassName:ShortMessageUtil.java
 * @Description: 短信工具类
 * @Author:lxt<839376636@qq.com>
 * @Date:2017年10月10日上午9:12:51
 * @Version:1.1.0
 */
public class ShortMessageUtil {
	/**
	 * 日志
	 */
	public static final Logger logger = LoggerFactory.getLogger(OperationLogService.class);

	private static final String PATH = "/short_message.properties";

	/**
	 * 
	 * @Title: getShoryMessageCode
	 * @Description: 获取短信验证码
	 * @return Integer
	 */
	public static Integer getShoryMessageCode() {
		return (int) (Math.random() * 9000 + 1000);
	}

	/**
	 * 
	 * @Title: sendShortMessageCode
	 * @Description: 发送短信工具类
	 * @param phoneNuber
	 * @return Integer
	 */
	public static Integer sendShortMessageCode(String phoneNumber) {
		// 获取短信验证码
		Integer code = getShoryMessageCode();
		logger.debug("发送短信，开始执行，发送目标：{}，验证码:{}", phoneNumber, code);
		/**
		 * 获取短信配置信息
		 */
		String uri = PropertiesUtil.getInstance().loadProperties(PATH, "uri");
		String cid = PropertiesUtil.getInstance().loadProperties(PATH, "cid");
		String key = PropertiesUtil.getInstance().loadProperties(PATH, "key");
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("cid", cid));
		params.add(new BasicNameValuePair("key", key));
		// 手机号
		params.add(new BasicNameValuePair("code", phoneNumber));
		// 验证码
		params.add(new BasicNameValuePair("msg", "您本次操作验证码为:" + code + "请及时输入"));
		HttpClientUtil.requestGet(uri, params);
		logger.debug("给{}，发送验证码：{}成功！", phoneNumber, code);
		return code;
	}

	/**
	 * 
	 * 
	 * <p>
	 * Title: sendReviceApplyMessage
	 * </p>
	 * 
	 * <p>
	 * Description: 接单后发送短信给科研人员
	 * </p>
	 * 
	 * @param phoneNumber
	 * @param patent
	 * @param scienceExp
	 * @param school
	 * @return
	 */
	public static Patent sendReviceApplyMessage(String phoneNumber, Patent patent, ScienceExpert scienceExp,
			String school) {

		logger.debug("发送接单提醒短信开始执行，发送目标:{},专利标题:{}，接单科技服务人员:{} {}的{} 联系电话:{}", phoneNumber, patent.getName(), school,
				scienceExp.getDepartment(), scienceExp.getPhoneNumber());
		String msg = "您的标题为" + patent.getName() + "的专利" + "已经被接单，接单科技服务人员:" + school + "  " + scienceExp.getDepartment()
				+ "的" + scienceExp.getUserName() + " 联系电话:" + scienceExp.getPhoneNumber();
		String uri = PropertiesUtil.getInstance().loadProperties(PATH, "uri");
		String cid = PropertiesUtil.getInstance().loadProperties(PATH, "cid");
		String key = PropertiesUtil.getInstance().loadProperties(PATH, "key");
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("cid", cid));
		params.add(new BasicNameValuePair("key", key));
		// 手机号
		params.add(new BasicNameValuePair("code", phoneNumber));
		// 验证码
		params.add(new BasicNameValuePair("msg", msg));
		HttpClientUtil.requestGet(uri, params);
		logger.debug("给{}，发送 {} 专利接单提醒成功！", phoneNumber, patent.getName());
		return patent;
	}

	
	/**
	 * 
	
	 * <p>Title: sendReviceApplyMessage</p>  
	
	 * <p>Description:结项后发送短信给科研人员</p>  
	
	 * @param phoneNumber
	 * @param patent
	 * @param scienceExp
	 * @param school
	 * @return
	 */
	public static Patent sendFinishPatentMessage(String phoneNumber, Patent patent, ScienceExpert scienceExp,
			String school) {

		logger.debug("发送结项提醒短信开始执行，发送目标:{},专利标题:{}，接单科技服务人员:{} {}的{} 联系电话:{}", phoneNumber, patent.getName(), school,
				scienceExp.getDepartment(), scienceExp.getPhoneNumber());
		String msg = "您的标题为" + patent.getName() + "的专利" + "已经结项，科技服务人员:" + school + "  " + scienceExp.getDepartment()
				+ "的" + scienceExp.getUserName() + " 联系电话:" + scienceExp.getPhoneNumber();
		String uri = PropertiesUtil.getInstance().loadProperties(PATH, "uri");
		String cid = PropertiesUtil.getInstance().loadProperties(PATH, "cid");
		String key = PropertiesUtil.getInstance().loadProperties(PATH, "key");
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("cid", cid));
		params.add(new BasicNameValuePair("key", key));
		// 手机号
		params.add(new BasicNameValuePair("code", phoneNumber));
		// 验证码
		params.add(new BasicNameValuePair("msg", msg));
		HttpClientUtil.requestGet(uri, params);
		logger.debug("给{}，发送 {} 专利结项提醒成功！", phoneNumber, patent.getName());
		return patent;
	}
	
	
	

}
